package com.codebase.zookeeper.curator;

import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.data.Stat;
import org.junit.Before;
import org.junit.Test;

public class Update {

    private CuratorFramework client;

    @Before
    public void before(){
        RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
        client = CuratorFrameworkFactory.builder()
                .connectString("127.0.0.1:2181")
                .sessionTimeoutMs(5000)
                .retryPolicy(retryPolicy)
                .build();
    }

    @Test
    public void test() throws Exception{
        String path = "/root";
        client.start();
        client.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL).forPath(path, "111".getBytes());
        Stat stat = new Stat();
        System.out.println("path:"+path+  "\t Data:" + new String(client.getData().storingStatIn(stat).forPath(path)) + "\t version:"+ stat.getVersion());

        int version = client.setData().withVersion(stat.getVersion()).forPath(path, "1111".getBytes()).getVersion();
        System.out.println("new version: " + version);
    }

}
